package com.littlebee.storychild.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 13/1/2016.
 */
public class DateTime extends Date {

    public DateTime(){
        super();
    }

    public DateTime(long milliseconds){
        super(milliseconds);
    }

    public DateTime(String date, String format){
        super(0);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date dd = sdf.parse(date);
            this.setTime(dd.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    /**
     * @return format String（yyyy-MM-dd HH:mm:ss）
     */
    public String toString() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(this);
    }
    /**
     * @param format
     * @return format String
     */
    public String toString(String format) {
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(this);
    }

    public int getCYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this);
        int year = calendar.get(Calendar.YEAR);
        return year;
    }

    public int getCMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this);
        int month = calendar.get(Calendar.MONTH);
        return month;
    }

    public int getCDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    public int getCWeek(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        return day;
    }

    public void AddDay(int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this);
        calendar.add(Calendar.DATE,day);
        this.setTime(calendar.getTimeInMillis());
    }

    public void setYMD(int year,int month,int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this);
        calendar.set(year,month,day);
        this.setTime(calendar.getTimeInMillis());
    }

    public  void setCYear(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this);
        calendar.set(Calendar.YEAR,year);
        this.setTime(calendar.getTimeInMillis());
    }

    public  void setCMonth(int month){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this);
        calendar.set(Calendar.MONTH,month);
        this.setTime(calendar.getTimeInMillis());
    }

    public  void setCDay(int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this);
        calendar.set(Calendar.DAY_OF_MONTH,day);
        this.setTime(calendar.getTimeInMillis());
    }

    public static String Format(Date date) {
        if (date == null) {
            return "";
        } else {
            return DateFormat.getDateInstance(DateFormat.MEDIUM).format(date);
        }
    }
}
